package com.sx.manage.util;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.common.util.NumberUtil;
import com.sx.manage.service.log.IMBaseSysOperLogService;
import com.sx.model.constants.type.function.MFuncType;
import com.sx.model.constants.type.function.MSystemType;
import com.sx.model.constants.type.log.MLogOperType;
import com.sx.model.manage.mybatis.po.log.MOperLogEntityPO;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;

/**
 * service impl 辅助类
 * 
 * @author lenovo
 *
 */
@Service("webLogUtil")
public class WebLogUtil {
	// 系统日志添加
	private Logger logger = LoggerFactory.getLogger(WebLogUtil.class);
	@Resource(name = "mBaseSysOperLogServiceImpl")
	private IMBaseSysOperLogService sysOperLogServiceImpl;

	/**
	 * 增加用户操作日志
	 * 
	 * @param data_id
	 * @param loginUser
	 * @param operType
	 * @param remark
	 */
	public void addOperLog(Long[] data_id, MConsumerLoginVO loginConsumer, MFuncType funcType, MLogOperType operType, String remark) {
		if (null != loginConsumer) {
			try {
				MOperLogEntityPO logPo = new MOperLogEntityPO();
				logPo.setIp_address(loginConsumer.getIp_address());
				logPo.setConsumer_name(loginConsumer.getConsumer_name());
				logPo.setConsumer_nature(loginConsumer.getConsumer_nature());
				logPo.setConsumer_id(loginConsumer.getConsumer_id());

				logPo.setLog_date(new Date());
				logPo.setModule(funcType);
				if (null != data_id && data_id.length > 0) {
					logPo.setOperator_content("数据id：" + NumberUtil.arrayToStr(data_id) + (StringUtils.isBlank(remark) ? "" : remark));
				} else {
					logPo.setOperator_content(remark);
				}
				logPo.setOperator_type(operType);
				logPo.setRemark(remark);
				logPo.setSystem_code(MSystemType.USER_CENTER);
				// 加入数据库
				sysOperLogServiceImpl.add(logPo);
			} catch (Exception ex) {
				logger.error("添加日志信息时出错：error={}", ex);
			}
		}
	}
}
